<template>
  <div class="standard-table">
    <div class="alert">
      <a-alert type="info" :show-icon="true" v-if="selectedRows">
        <div class="message" slot="message">
          已选择&nbsp;
          <a>{{ selectedRows.length }}</a>&nbsp;项
          <a class="clear" @click="onClear">清空</a>
        </div>
      </a-alert>
    </div>
    <a-table :loading="loading" :columns="columns" :bordered="bordered" :dataSource="dataSource" :rowKey="rowKey"
      :pagination="pagination" :expandedRowKeys="expandedRowKeys" :expandedRowRender="expandedRowRender"
      :scroll="scroll" @change="onChange"
      :rowSelection="selectedRows ? { selectedRowKeys: selectedRowKeys, onChange: updateSelect } : undefined"
      :rowClassName="rowClassName">
      <template slot-scope="text, record, index"
        v-for="slot in Object.keys($scopedSlots).filter(key => key !== 'expandedRowRender')" :slot="slot">
        <slot :name="slot" v-bind="{ text, record, index }"></slot>
      </template>
      <template :slot="slot" v-for="slot in Object.keys($slots)">
        <slot :name="slot"></slot>
      </template>
      <template slot-scope="{ record, index, indent, expanded }"
        :slot="$scopedSlots.expandedRowRender ? 'expandedRowRender' : ''">
        <slot v-bind="{ record, index, indent, expanded }"
          :name="$scopedSlots.expandedRowRender ? 'expandedRowRender' : ''"></slot>
      </template>
    </a-table>
  </div>
</template>

<script>
export default {
  name: 'StandardTable',
  props: {
    loading: [Boolean, Object],
    columns: Array,
    dataSource: Array,
    rowKey: {
      type: [String, Function],
      default: 'key',
    },
    pagination: {
      type: [Object, Boolean],
      default: true,
    },
    bordered: {
      type: Boolean,
      default: false,
    },
    selectedRows: Array,
    expandedRowKeys: Array,
    expandedRowRender: Function,
    scroll: Object,
  },
  data() {
    return {};
  },

  methods: {
    rowClassName(row) {
      if (row.manualInstitutionResult) {
        return 'bgc_red'
      }
    },
    updateSelect(selectedRowKeys, selectedRows) {
      this.$emit('update:selectedRows', selectedRows);
      this.$emit('selectedRowChange', selectedRowKeys, selectedRows);
    },
    initTotalList(columns) {
      const totalList = columns
        .filter((item) => item.needTotal)
        .map((item) => {
          return {
            ...item,
            total: 0,
          };
        });
      return totalList;
    },
    onClear() {
      this.updateSelect([], []);
      this.$emit('clear');
    },
    onChange(pagination, filters, sorter, { currentDataSource }) {
      this.$emit('change', pagination, filters, sorter, { currentDataSource });
    },
  },
  watch: {
    dataSource: {
      handler() {
        this.dataSource.map((current, index) => {
          current.key = index;
        });
      },
      deep: true,
    },
  },
  computed: {
    computedDataSource() {
    // 创建一个新的数组并返回，确保 Vue 的响应式能够检测到变化
    return this.dataSource.map(row => ({
      ...row
    }));
  },
    selectedRowKeys() {
      return this.selectedRows.map((record) => {
        return typeof this.rowKey === 'function'
          ? this.rowKey(record)
          : record[this.rowKey];
      });
    },
  },
};
</script>

<style scoped lang="scss">
.standard-table {
  .alert {
    margin-bottom: 16px;

    .message {
      a {
        font-weight: 600;
      }
    }

    .clear {
      float: right;
    }
  }

  .even-row {
    background-color: #ffebeb;
    /* Light red background for even rows */
  }
}

:deep(.bgc_red) {
  background-color: skyblue !important;
}
</style>
